<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-form
      :model="loginForm"
      :rules="rules"
      ref="loginForm"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username" style="width: 380px">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" style="width: 380px">
        <el-input v-model="loginForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('loginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>
<script>
import { login } from "@/api/login";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "zxguan",
        password: "123456",
        code: "",
        token: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      captchaImg: null,
    };
  },
  created() {
    // localStorage.clear()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            username: this.loginForm.username,
            password: this.loginForm.password,
          };
          login(data)
            .then((res) => {
              console.log(res);
              this.$store.commit("SET_TOKEN", res);
              this.$router.push("/index");
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // getCaptcha() {
    //   this.$axios.get("/captcha").then((res) => {
    //     console.log("/captcha");
    //     console.log(res);
    //     this.loginForm.token = res.data.data.token;
    //     this.captchaImg = res.data.data.captchaImg;
    //     this.loginForm.code = "";
    //   });
    // },
  },
};
</script>
<style scoped>
.el-row {
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}
.el-divider {
  height: 200px;
}
.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
